package com.dx_society.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dx_society.domain.Activities;
import com.dx_society.domain.ActivityComment;
import com.dx_society.dto.Result;
import com.dx_society.service.ActivityCommentService;
import com.dx_society.mapper.ActivityCommentMapper;
import com.dx_society.service.MessageService;
import com.dx_society.utils.UserHolder;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
* @author 韶光善良君
* @description 针对表【dx_activity_comment(活动评论表)】的数据库操作Service实现
* @createDate 2024-07-01 10:15:07
*/
@Service
public class ActivityCommentServiceImpl extends ServiceImpl<ActivityCommentMapper, ActivityComment>
    implements ActivityCommentService{

    @Resource
    private MessageService messageService;
    @Override
    public Result sendComment(Long id, String comment) {
        Long userId;
        try {
            userId = UserHolder.getUser().getUserid();
        } catch (Exception e) {
            return Result.fail("用户未登录");
        }
        ActivityComment activityComment = new ActivityComment();
        activityComment.setSendUserId(userId);
        activityComment.setAcId(id);
        activityComment.setSendUserName(UserHolder.getUser().getUsername());
        activityComment.setContext(comment);
        activityComment.setSendtime(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));
        activityComment.setState(0);
        if (save(activityComment)) {
            return Result.ok("发送成功");
        }
        return Result.fail("评论失败");
    }

    @Override
    public Result queryComment(Long id) {
        List<ActivityComment> commentList = query().eq("ac_id", id).eq("state", 1).list();
        if(commentList.isEmpty()){
            return Result.fail("该活动暂时没有评论");
        }
        return Result.ok(commentList);
    }

    @Override
    public Result agreeMyAcComment(Long id, Boolean agree) {
        ActivityComment activityComment = getById(id);
        if (agree) {
            activityComment.setState(1);

            messageService.sendMessage(activityComment.getSendUserId(),"您的评论\""+activityComment.getContext()+"\" 已过审");
        } else {
            activityComment.setState(2);

            messageService.sendMessage(activityComment.getSendUserId(),"您的评论\""+activityComment.getContext()+"\" 被驳回");
        }
        if (updateById(activityComment)) {
            return Result.ok("操作成功");
        }else {
            return Result.fail("操作失败");
        }
    }

    @Override
    public Result deleteMyAcComment(Long id) {
        if (removeById(id)) {
            return Result.ok("删除成功");
        }
        return Result.fail("删除失败");
    }


}




